import 'package:flutter/material.dart';
import 'package:light_image/pages/guide_page.dart';
import 'package:light_image/pages/login_page.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'my_app.dart';

//void main() => runApp(MyApp());

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:light_image/config/storage_manager.dart';

import 'pages/root_page.dart';

Future<String> readPhone() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String ph = prefs.getString('phone');
  return ph;
}

Future<bool> isGuided() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool ph = prefs.getBool('Guided');
  if (ph == null) {
    ph = false;
  }
  return ph;
}

/**
 * 获取开始首页
 */
Future<StatefulWidget> getStartHomePage() async {
  var guidedValue = await isGuided();
  if (guidedValue != null && guidedValue == true) {
     String phone=  await readPhone()  ;
      //直接进入
      if (phone != null && phone.isNotEmpty) {
        print("首页");
        return   RootPage();
      } else {
        print('登录');
        return   LoginPage();
      }
  } else {
    print("引导");
    return GuidePage();
  }
}

/*
* App入口
* */
void main() async {
  /// 确保初始化
  WidgetsFlutterBinding.ensureInitialized();

  /// 配置初始化
  await StorageManager.init();
  var startHomePage = getStartHomePage();
  startHomePage.then((onValue) {
    print('程序执行应该是' + onValue.toString());
    runApp(MyApp(onValue));
    print('程序启动完成' + onValue.toString());
  });

  /// 自定义报错页面
  ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
    debugPrint(flutterErrorDetails.toString());
    return new Center(child: new Text("App错误，快去反馈给作者!"));
  };

  /// Android状态栏透明
//  if (Platform.isAndroid) {
//    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
//      statusBarColor: Colors.transparent,
//      statusBarBrightness: Brightness.light,
//    );
//    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
//  }
}
